package com.skivingcloud.report.mng.service.impl;

import com.skivingcloud.admin.sys.entity.User;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.UuidUtil;
import com.skivingcloud.report.bigscreen.entity.ReportDashboard;
import com.skivingcloud.report.bigscreen.entity.ReportDashboardWidget;
import com.skivingcloud.report.bigscreen.mapper.ReportDashboardMapper;
import com.skivingcloud.report.bigscreen.mapper.ReportDashboardWidgetMapper;
import com.skivingcloud.report.mng.entity.Report;
import com.skivingcloud.report.mng.mapper.ReportMapper;
import com.skivingcloud.report.mng.service.IReportService;

import net.sf.json.JSONObject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.time.LocalDateTime;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 大屏报表 服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-07-28
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements IReportService {

	@Autowired
	private ReportDashboardMapper reportDashboardMapper;
	@Autowired
	private ReportDashboardWidgetMapper reportDashboardWidgetMapper;
	
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils = PageUtils.getPageInfo(params);
		Page<Report> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		Report report = (Report) JSONObject.toBean(
				JSONObject.fromObject(params.get("dataForm")),
				Report.class);
		QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isNotBlank(report.getReportType())) {
			queryWrapper.eq("report_type", report.getReportType());
		}
		if (StringUtils.isNotBlank(report.getReportCode())) {
			queryWrapper.like("report_code", "%" + report.getReportCode() + "%");
		}
		if (StringUtils.isNotBlank(report.getReportName())) {
			queryWrapper.like("report_name", "%" + report.getReportName() + "%");
		}
		this.baseMapper.selectList(queryWrapper);
		pageUtils = new PageUtils(p);
		return pageUtils;
	}

	@Override
	public Report selectById(String id) {
		return this.baseMapper.selectById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
    public void add(Report report, User user) {
		report.setId(UuidUtil.get32UUID());
        report.setEnableFlag("1");
        report.setDeleteFlag("0");
        report.setCreater(user.getId());
        report.setCreateTime(LocalDateTime.now());
		report.setVersion(0);
		this.baseMapper.insert(report);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
    public void update(Report report, User user) {
		report.setModUser(user.getId());
        report.setModTime(LocalDateTime.now());
		report.setVersion(report.getVersion() + 1);
        this.baseMapper.updateById(report);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
    public void deleteBatch(String[] ids) {
		for(String id : ids){
			Report report = this.baseMapper.selectById(id);
			reportDashboardMapper.delete(new QueryWrapper<ReportDashboard>().lambda().eq(ReportDashboard :: getReportCode, report.getReportCode()));
			reportDashboardWidgetMapper.delete(new QueryWrapper<ReportDashboardWidget>().lambda().eq(ReportDashboardWidget :: getReportCode, report.getReportCode()));
			// TODO 先删除大屏报表和excel报表报表再删除主表数据，等实现excel报表再来补充
			this.baseMapper.deleteById(id);
		}
		
	}

}
